package com.library.dao;

import com.library.entity.UserInfo;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

//import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ReaderInfoDao {

    private final static String NAMESPACE = "com.library.dao.ReaderInfoDao.";
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public ArrayList<UserInfo> getAllReaderInfo() {
        List<UserInfo> result = sqlSessionTemplate.selectList(NAMESPACE + "getAllReaderInfo");

        return (ArrayList<UserInfo>) result;
    }

    public UserInfo findReaderInfoByReaderId(final long reader_id) {
        return sqlSessionTemplate.selectOne(NAMESPACE + "findReaderInfoByReaderId", reader_id);
    }

    public int deleteReaderInfo(final long reader_id) {
        return sqlSessionTemplate.delete(NAMESPACE + "deleteReaderInfo", reader_id);
    }

    public int editReaderInfo(final UserInfo userInfo) {
        return sqlSessionTemplate.update(NAMESPACE + "editReaderInfo", userInfo);
    }

    public int editReaderCard(final UserInfo userInfo) {
        return sqlSessionTemplate.update(NAMESPACE + "editReaderCard", userInfo);
    }

    public final long addReaderInfo(final UserInfo userInfo) {
        if (sqlSessionTemplate.insert(NAMESPACE + "addReaderInfo", userInfo) > 0) {
            return this.getReaderId(userInfo);
        } else {
            return -1;
        }
    }

    public final long getReaderId(final UserInfo userInfo) {
        return sqlSessionTemplate.selectOne(NAMESPACE + "getReaderId", userInfo);
    }

}
